// pages/contentAdd/contentAdd.js
const {
    addContent
} = require('../../services/contentAddReq')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        titleValue: '',
        commentValue: '',
        activityId: 0,
        fileList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const id = options.id
        this.setData({
            activityId: id
        })
    },


    // 发布活动风采
    handleAddNow() {

        const volunteerId = wx.getStorageSync('volunteerInfo').volunteerId;

        let activityId = this.data.activityId;
        let commentValue = this.data.commentValue;
        let fileList = this.data.fileList;
        let titleValue = this.data.titleValue;
        let token = wx.getStorageSync('token');

        if (!volunteerId) {
            return wx.showToast({
                title: '请登陆',
                icon: "error",
                duration: 2000,
                success(res) {
                    return wx.redirectTo({
                        url: '../login/login'
                    })
                }
            })
        }

        if (titleValue === '') {
            return wx.showToast({
                title: '请输入标题',
                icon: 'none'
            })
        }
        if (commentValue === '') {
            return wx.showToast({
                title: '请输入正文',
                icon: 'none'
            })
        }
        let tmpImg = []
        for (let each of fileList) {
            let {
                url
            } = each;
            console.log(url)
            setTimeout(()=>wx.uploadFile({
                url: 'https://www.16tongxing.net/upload/files',
                filePath: url,
                header: {
                    'Authorization': token
                },
                name: 'files',
                success(res) {
                    try {
                        let tmpUrl = `<img  src="${JSON.parse(res.data).files[0]}" style="display:block;width:100%;height:200px;" alt="手机发布的图片"/>`
                        tmpImg.push(tmpUrl)
                        if (tmpImg.length === fileList.length) {
                        console.log(tmpImg.length === fileList.length)

                            commentValue = `${tmpImg.join(" ")}<p>${commentValue}</p>`
                            addContent(activityId, commentValue, titleValue, volunteerId, res => {
                                if (res.data.code !== 0) {
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'none'
                                    })
                                } else {
                                    wx.showToast({
                                        title: '发布成功',
                                    })
                                    wx.navigateBack({
                                        delta: 1,
                                    })
                                }
                            })
                        }
                    } catch (e) {

                    }

                },
                fail(err) {
                    console.log(err)
                }
            }))
        }
    },

    handleCommentChange(e) {
        this.setData({
            commentValue: e.detail.value
        })
    },

    handleTitleInput(e) {
        this.setData({
            titleValue: e.detail.value
        })
    },

    afterRead(event) {
        const {
            file
        } = event.detail;

        let fileList = this.data.fileList;
        fileList.push({
            ...file,
            url: file.url
        });
        this.setData({
            fileList
        });
    },

    handleDelete(e) {
        let {
            index
        } = e.detail;

        let fileList = this.data.fileList;
        fileList.splice(index, 1)
        this.setData({
            fileList
        })
    },
})